(function (mui, $) {
	
var _Data = {
	'token'       : app.store('token'),
	'page'        : 0,
	'nums'        : 20,
	'order'       : {'column':'', 'type':''},
	'category_id' : '',
	'keywords'    : '',
	'price'       : {'max':'', 'min':''}
};

function next (cb) {
	app.log('请求参数：', mui.extend({}, _Data, {'page':_Data.page + 1}));
	if (plus.webview.currentWebview().isVisible()) { plus.nativeUI.showWaiting(); }
	$.ajax({
		'dataType' : 'json',
		'type'     : 'post',
		'url'      : app.url('App/Goods/Index/lists'),
		'data'     : mui.extend({}, _Data, {'page':_Data.page + 1}),
	})
	.fail(function (res) {
		// 失败回调
		plus.nativeUI.closeWaiting();
		app.log('取得商品列表失败：', res);
		app.tryResetPullLoading();
	})
	.done(function (res) {
		// 成功回调
		plus.nativeUI.closeWaiting();
		app.log('商品列表', res);
		
		if (500 == res.code) { app.toast('你还未登录'); app.tryResetPullLoading(); return; }
		if (300 == res.code) { app.toast(res.msg); return; }
		if (200 != res.code) { app.toast('请求失败'); return; }
		if (res.msg && plus.webview.currentWebview().isVisible()) { app.toast(res.msg); }
		
		if (res.data && res.data.length) { _Data.page++; }
		cb && cb(res);
	})
	;
}

function call_on_pull_down () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	_Data.page = 0;
	$(pullrefresh.element).find('.mui-scroll>.scroll-data').empty();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		$(dom).append(app.tpl($(dom).attr('data-tpl'), res));
		pullrefresh.endPulldownToRefresh();
	});
}
function call_on_pull_up () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		$(dom).append(app.tpl($(dom).attr('data-tpl'), res));
		pullrefresh.endPullupToRefresh();
	});
}
mui.init({
	pullRefresh   : {
		container : '.container-pull-refresh',
		down      : {callback : call_on_pull_down},
		up        : {callback : call_on_pull_up},
	}
});

var page = window.page = {};
page.init = function (data) {
	data = data || {};
	app.log('刷新：', data);
	_Data.page        = 0;
	_Data.keywords    = undefined === data.keyword     ? _Data.keywords    : data.keyword;
	_Data.category_id = undefined === data.category_id ? _Data.category_id : data.category_id;
	_Data.price       = undefined === data.price       ? _Data.price       : data.price;
	
	call_on_pull_down();
}

mui.plusReady(function() {
	var view = plus.webview.currentWebview();
	
	setTimeout(function () {
		page.init(plus.webview.currentWebview().extras);
	},1000);
	
	setTimeout(function () {
		var link  = 'product.search.scroll.filter.html';
		var child = plus.webview.create(link, link, {top:'43px', bottom:'0px'}, {'extras':view.extras});
		child.hide();
		view.append(child);
	}, 1500);
});

// 普通页面跳转
$('body').delegate('[data-url]', 'tap', function () {
	app.open($(this).attr('data-url'));
});

// 查看商品详情
$('body').delegate('.product', 'tap', function () {
	app.open('product.detail.html', {'id':$(this).attr('data-id')});
});

template.helper('thumb', function (url) {
	var idx = url.lastIndexOf('.');
	var zip = url.substr(0, idx) + '_200_200' + url.substr(idx);
	
	return url ? (app.link.image + zip) : '../image/icon-no-image.png';
});
// 显示排序下拉内容
$(".search-menu > div:not('.filter')").on("tap", function(){
	var target = $(this).attr("data-target");
	$(target).stop().slideToggle(200).siblings(".search").hide();
	
	var link = 'product.search.scroll.filter.html';
	var view = plus.webview.getWebviewById(link);
	if (view.isVisible()) { view.hide(); }
});
// 显示筛选子页面
$('.filter').on('tap', function () {
	var link = 'product.search.scroll.filter.html';
	var view = plus.webview.getWebviewById(link);
	
	if (view.isVisible()) { view.hide(); } else { view.show('slide-in-bottom', 200); }
	$('.search').hide();
});

//点击 菜单前三项下面的下拉  添加勾
$(".search > div").on("tap", function(){
	var dom = this;
	$('.search').hide().find("i").hide();
	$(dom).find("i").show();
	// 更新排序规则
	_Data.order.column = $(dom).attr('data-column');
	_Data.order.type = $(dom).attr('data-type');
	
	call_on_pull_down();
})

//点击筛选 - 价格范围  修改价格区间 价格 
$(".search-interval span").on("tap",function(){
	var a = $(this).find("a label:first-child").text()
	$(".search-interval input:nth-child(2)").val(a);
	
	a = $(this).find("a label:last-child").text()
	$(".search-interval input:nth-child(4)").val(a);
})

//修改input中内容的时候 将变红的radio变成灰色
$(".search-interval input").on("change", function(){
	var a = $(".search-interval input").val();
	if(isNaN(a)){
		alert("请输入数字！~");
		$(".search-interval input").val(0);
	}
	$(this).blur(function(){
		$(".search-screen input[type=radio]").prop("checked", false);
	})
});

// 确定筛选数据
$('.search-screen .btn-submit').on('tap', function () {
	_Data.price.min = $('.price-min').val();
	_Data.price.max = $('.price-max').val();
	
	$('.search-screen').hide();
	call_on_pull_down();
});

$('[data-show-type]').on('tap', function (e) {
	e.stopPropagation();
	$('.product-list').toggleClass('one-row');
	$('.search-comprehensive').hide();
});

})(mui, $);
